package com.mini.framework.db.dbutil;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class OraclePageQueryer implements PageQueryer {

	@Override
	public void pageQuery(String sql, QueryRunner runner, Class<?> clazz,
			int pageNo, int pageSize, Page page, Object... params) {
		StringBuilder allSql = new StringBuilder();
		String before = " SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (";
		allSql.append(before);
		allSql.append(sql);
		String after  = ") A WHERE ROWNUM  <=" + page.getEnd() +")  WHERE RN  >" + page.getStart();
		allSql.append(after);
		try{
			List<? extends Object> list = (List<? extends Object>) runner.query(allSql.toString(), new BeanListHandler(clazz),params);
			page.setResult(list);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
